/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.grid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.taglib.TagUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.util.Collection;

/**
 * <p>Tag to fetch rows of grid. Each row represents a piece of data.
 * <br />
 * This tag is only valid when nested within <em>grid</em> tag
 * </p>
 * <p>
 * Allowed attributes are:
 * <ul>
 * <li>
 * <b>beanId</b> - identifier of Spring bean to use for getting rows for this
 * grid
 * </li>
 * <li>
 * <b>method</b> - method of specified bean to call to get collection. This
 * method must return instance of
 * {@link com.blandware.atleap.common.util.PartialCollection} and
 * accept instance of {@link com.blandware.atleap.common.util.QueryInfo}
 * as a single argument.
 * </li>
 * <li>
 * <b>collection</b> - collection of objects to display if <b>beanId</b> or
 * <b>method</b> not
 * specified. Only instances of <cody>java.util.Collection</code>,
 * </code>java.lang.Object[]</code> and <code>java.util.Map</code> are
 * supported.
 * </li>
 * <li>
 * <b>tagId</b> - identifier for this tag. It must be unique within
 * one grid.
 * </li>
 * <li>
 * <b>var</b> - required - name of variable to save result
 * </li>
 * <li>
 * <b>scope</b> - scope of variable to save result
 * </li>
 * </ul>
 * </p>
 * <p>
 * Either <b>collection</b> or both <b>beanId</b> and <b>method</b> must be specified.
 * If <b>beanId</b> and <b>method</b> are specified, collection will be
 * obtained invoking <b>method</b> on bean,
 * which ID is <b>beanId</b>. Else, <b>collection</b> will be used as such
 * collection.
 * </p>
 * <p><a href="FetchRowsTag.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.4 $ $Date: 2007/03/29 10:37:42 $
 * @jsp.tag name="fetchRows"
 * body-content="empty"
 */
public class FetchRowsTag extends FetchRowsSupport {

    protected transient final Log log = LogFactory.getLog(FetchRowsTag.class);

    /**
	 * Name of page-scoped variable to save rows under
	 */
	protected String var;

    /**
     * Scope under which rows will be saved
     */
    protected String scope;

    /**
     * Returns name of page-scoped variable to save rows under.
     *
     * @return variable name
     * @see #var
     * @jsp.attribute required="true"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Name of variable to save rows under"
     */
    public String getVar() {
        return var;
    }

    /**
     * Set name of page-scoped variable to save rows under.
     *
     * @param var variable name to set
     */
    public void setVar(String var) {
        this.var = var;
    }

    /**
     * Returns scope under which rows will be saved.
     *
     * @return variable scope
     * @see #scope
     * @jsp.attribute required="false"
	 * rtexprvalue="true"
	 * type="java.lang.String"
	 * description="Scope of variable to save rows"
     */
    public String getScope() {
        return scope;
    }

    /**
     * Sets scope under which rows will be saved.
     *
     * @param scope scope to set
     */
    public void setScope(String scope) {
        this.scope = scope;
    }

    /**
     * Makes main tag job: fetches rows and puts them into context.
     */
    public void doTag() throws JspException, IOException {
		PageContext pageContext = (PageContext) getJspContext();

        GridTag parentGridTag = getParentGridTag();

        Collection rows = fetchCollection(parentGridTag, pageContext, false);

        TagUtils tagUtils = TagUtils.getInstance();
        int varScope = PageContext.PAGE_SCOPE;
        if ( scope != null ) {
            varScope = tagUtils.getScope(scope);
        }
        pageContext.setAttribute(var, rows, varScope);
    }
}
